package com.example.rwq_test.stateMonitor;

import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Description 状态监控器
 *
 * @Author renweiqiang
 * @Date 2024/4/23 下午6:08
 */
public class StateExpectAndRealValueMonitor implements Runnable {
    /**
     * 状态监控枚举
     */
    private StateMonitorEnum stateMonitorEnum;
    /**
     * 锁
     */
    private CountDownLatch countDownLatch;
    /**
     * 监控结束时间
     */
    private Long monitorEndTime;

    public StateExpectAndRealValueMonitor(StateMonitorEnum stateMonitorEnum, CountDownLatch countDownLatch) {
        this.stateMonitorEnum = stateMonitorEnum;
        this.countDownLatch = countDownLatch;
        this.monitorEndTime = System.currentTimeMillis() + stateMonitorEnum.getMonitorTime();
    }

    @Override
    public void run() {
        while (System.currentTimeMillis() < monitorEndTime && countDownLatch.getCount() > 0) {
            //未到线程结束时间
            switch (stateMonitorEnum) {
                case TASK_FINISH:
                    if (Objects.equals(TaskState.state, stateMonitorEnum.getExpectValue())) {
                        countDownLatch.countDown();
                    }
                    break;
                case BUSINESS_STATE:
                    // todo
                    break;
                case VEHICLE_STATE:
                    // todo
                    break;
                default:
                    System.out.println("worn...");
            }

            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
